{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 心脏病预测\n",
    "### 导入数据"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   age  sex  cp  trestbps  chol  fbs  restecg  thalach  exang  oldpeak  slope  \\\n",
      "0   63    1   1       145   233    1        2      150      0      2.3      3   \n",
      "1   67    1   4       160   286    0        2      108      1      1.5      2   \n",
      "2   67    1   4       120   229    0        2      129      1      2.6      2   \n",
      "3   37    1   3       130   250    0        0      187      0      3.5      3   \n",
      "4   41    0   2       130   204    0        2      172      0      1.4      1   \n",
      "\n",
      "   ca        thal  target  \n",
      "0   0       fixed       0  \n",
      "1   3      normal       1  \n",
      "2   2  reversible       0  \n",
      "3   0      normal       0  \n",
      "4   0      normal       0  \n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "filename = \"F:/机器学习数据集/心脏病预测/heart/heart.csv\"\n",
    "data = pd.read_csv(filename)\n",
    "print(data[:5])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-09T12:54:51.615095900Z",
     "start_time": "2023-10-09T12:54:50.282537800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   age  sex  cp  trestbps  chol  fbs  restecg  thalach  exang  oldpeak  slope  \\\n",
      "0   63    1   1       145   233    1        2      150      0      2.3      3   \n",
      "1   67    1   4       160   286    0        2      108      1      1.5      2   \n",
      "2   67    1   4       120   229    0        2      129      1      2.6      2   \n",
      "3   37    1   3       130   250    0        0      187      0      3.5      3   \n",
      "4   41    0   2       130   204    0        2      172      0      1.4      1   \n",
      "\n",
      "   ca  thal  target  \n",
      "0   0     2       0  \n",
      "1   3     3       1  \n",
      "2   2     4       0  \n",
      "3   0     3       0  \n",
      "4   0     3       0  \n"
     ]
    }
   ],
   "source": [
    "# 编码分类型变量\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "df1 = data.copy()\n",
    "le = LabelEncoder()\n",
    "le.fit(df1['thal'])\n",
    "df1['thal'] = le.transform(df1['thal'])\n",
    "print(df1[:5])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-09T12:59:58.294767700Z",
     "start_time": "2023-10-09T12:59:52.141244100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 63.    1.    1.  145.  233.    1.    2.  150.    0.    2.3   3.    0.\n",
      "    2. ]\n",
      " [ 67.    1.    4.  160.  286.    0.    2.  108.    1.    1.5   2.    3.\n",
      "    3. ]\n",
      " [ 67.    1.    4.  120.  229.    0.    2.  129.    1.    2.6   2.    2.\n",
      "    4. ]\n",
      " [ 37.    1.    3.  130.  250.    0.    0.  187.    0.    3.5   3.    0.\n",
      "    3. ]\n",
      " [ 41.    0.    2.  130.  204.    0.    2.  172.    0.    1.4   1.    0.\n",
      "    3. ]]\n",
      "[0. 1. 0. 0. 0.]\n"
     ]
    }
   ],
   "source": [
    "# 划分X与y\n",
    "data = np.array(df1)\n",
    "X = data[:,:-1]\n",
    "y = data[:,-1]\n",
    "print(X[:5])\n",
    "print(y[:5])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-09T13:00:11.464609Z",
     "start_time": "2023-10-09T13:00:11.403610500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集精度 1.0\n",
      "测试集精度 0.776\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "\n",
    "X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=0,shuffle=True)\n",
    "tree_class = DecisionTreeClassifier(max_depth=10)\n",
    "tree_class.fit(X_train,y_train)\n",
    "print(\"训练集精度\",round(tree_class.score(X_train,y_train),3))\n",
    "print(\"测试集精度\",round(tree_class.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-09T13:00:16.486462700Z",
     "start_time": "2023-10-09T13:00:14.355792800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度: 0.8421052631578947\n"
     ]
    }
   ],
   "source": [
    "from sklearn.svm import SVC\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "\n",
    "pipe_svc = Pipeline([('scaler',MinMaxScaler()),('svc',SVC())])\n",
    "pipe_svc.fit(X_train,y_train)\n",
    "print(\"测试集精度:\",pipe_svc.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-09T13:02:53.475206700Z",
     "start_time": "2023-10-09T13:02:53.343220100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合 {'svc__C': 1, 'svc__gamma': 0.1, 'svc__kernel': 'rbf'}\n",
      "最佳验证集精度 0.8369082125603864\n",
      "测试集精度 0.8289473684210527\n"
     ]
    }
   ],
   "source": [
    "# 调整参数\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "pipe_svc = Pipeline([('scaler',MinMaxScaler()),('svc',SVC())])\n",
    "params_grid={'svc__gamma':[0.001,0.01,0.1,1],'svc__C':[0.01,0.1,1,10],'svc__kernel':['rbf','linear']}\n",
    "grid_svc = GridSearchCV(pipe_svc,param_grid=params_grid)\n",
    "grid_svc.fit(X_train,y_train)\n",
    "print('最佳参数组合',grid_svc.best_params_)\n",
    "print(\"最佳验证集精度\",grid_svc.best_score_)\n",
    "print(\"测试集精度\",grid_svc.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-09T13:11:53.115967800Z",
     "start_time": "2023-10-09T13:11:52.584970Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度: 0.8157894736842105\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "rf = RandomForestClassifier()\n",
    "rf.fit(X_train,y_train)\n",
    "print(\"测试集精度:\",rf.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-09T13:05:03.266639600Z",
     "start_time": "2023-10-09T13:05:02.656185900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度: 0.7631578947368421\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "grad = GradientBoostingClassifier()\n",
    "grad.fit(X_train,y_train)\n",
    "print(\"测试集精度:\",grad.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-09T13:05:52.925645200Z",
     "start_time": "2023-10-09T13:05:52.661862300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   age  sex  cp  trestbps  chol  fbs  restecg  thalach  exang  oldpeak  slope  \\\n",
      "0   63    1   1       145   233    1        2      150      0      2.3      3   \n",
      "1   67    1   4       160   286    0        2      108      1      1.5      2   \n",
      "2   67    1   4       120   229    0        2      129      1      2.6      2   \n",
      "3   37    1   3       130   250    0        0      187      0      3.5      3   \n",
      "4   41    0   2       130   204    0        2      172      0      1.4      1   \n",
      "\n",
      "   ca        thal  target  \n",
      "0   0       fixed       0  \n",
      "1   3      normal       1  \n",
      "2   2  reversible       0  \n",
      "3   0      normal       0  \n",
      "4   0      normal       0  \n"
     ]
    }
   ],
   "source": [
    "# 尝试做独热编码\n",
    "import pandas as pd\n",
    "filename = \"F:/机器学习数据集/心脏病预测/heart/heart.csv\"\n",
    "data = pd.read_csv(filename)\n",
    "print(data[:5])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T05:12:11.966836100Z",
     "start_time": "2023-10-10T05:12:10.999145200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   age  sex  cp  trestbps  chol  fbs  restecg  thalach  exang  oldpeak  slope  \\\n",
      "0   63    1   1       145   233    1        2      150      0      2.3      3   \n",
      "1   67    1   4       160   286    0        2      108      1      1.5      2   \n",
      "2   67    1   4       120   229    0        2      129      1      2.6      2   \n",
      "3   37    1   3       130   250    0        0      187      0      3.5      3   \n",
      "4   41    0   2       130   204    0        2      172      0      1.4      1   \n",
      "\n",
      "   ca        thal  \n",
      "0   0       fixed  \n",
      "1   3      normal  \n",
      "2   2  reversible  \n",
      "3   0      normal  \n",
      "4   0      normal  \n",
      "0    0\n",
      "1    1\n",
      "2    0\n",
      "3    0\n",
      "4    0\n",
      "Name: target, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "df_inputs = data.iloc[:,0:-1]\n",
    "df_labels = data.iloc[:,-1]\n",
    "print(df_inputs.iloc[:5])\n",
    "print(df_labels.iloc[:5])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T05:12:26.491693500Z",
     "start_time": "2023-10-10T05:12:26.455700400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "# 编码分类型变量\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "le = LabelEncoder()\n",
    "le.fit(df_inputs['thal'])\n",
    "df_inputs['thal'] = le.transform(df_inputs['thal'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T05:12:49.328031900Z",
     "start_time": "2023-10-10T05:12:43.695967800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "   age  sex  cp  trestbps  chol  fbs  restecg  thalach  exang  oldpeak  slope  \\\n0   63    1   1       145   233    1        2      150      0      2.3      3   \n1   67    1   4       160   286    0        2      108      1      1.5      2   \n2   67    1   4       120   229    0        2      129      1      2.6      2   \n3   37    1   3       130   250    0        0      187      0      3.5      3   \n4   41    0   2       130   204    0        2      172      0      1.4      1   \n\n   ca  thal  \n0   0     2  \n1   3     3  \n2   2     4  \n3   0     3  \n4   0     3  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>age</th>\n      <th>sex</th>\n      <th>cp</th>\n      <th>trestbps</th>\n      <th>chol</th>\n      <th>fbs</th>\n      <th>restecg</th>\n      <th>thalach</th>\n      <th>exang</th>\n      <th>oldpeak</th>\n      <th>slope</th>\n      <th>ca</th>\n      <th>thal</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>63</td>\n      <td>1</td>\n      <td>1</td>\n      <td>145</td>\n      <td>233</td>\n      <td>1</td>\n      <td>2</td>\n      <td>150</td>\n      <td>0</td>\n      <td>2.3</td>\n      <td>3</td>\n      <td>0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>67</td>\n      <td>1</td>\n      <td>4</td>\n      <td>160</td>\n      <td>286</td>\n      <td>0</td>\n      <td>2</td>\n      <td>108</td>\n      <td>1</td>\n      <td>1.5</td>\n      <td>2</td>\n      <td>3</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>67</td>\n      <td>1</td>\n      <td>4</td>\n      <td>120</td>\n      <td>229</td>\n      <td>0</td>\n      <td>2</td>\n      <td>129</td>\n      <td>1</td>\n      <td>2.6</td>\n      <td>2</td>\n      <td>2</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>37</td>\n      <td>1</td>\n      <td>3</td>\n      <td>130</td>\n      <td>250</td>\n      <td>0</td>\n      <td>0</td>\n      <td>187</td>\n      <td>0</td>\n      <td>3.5</td>\n      <td>3</td>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>41</td>\n      <td>0</td>\n      <td>2</td>\n      <td>130</td>\n      <td>204</td>\n      <td>0</td>\n      <td>2</td>\n      <td>172</td>\n      <td>0</td>\n      <td>1.4</td>\n      <td>1</td>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_inputs[:5]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T05:12:52.180465800Z",
     "start_time": "2023-10-10T05:12:52.089471900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "   age  trestbps  chol  thalach  oldpeak  sex__0  sex__1  cp__0  cp__1  cp__2  \\\n0   63       145   233      150      2.3       0       1      0      1      0   \n1   67       160   286      108      1.5       0       1      0      0      0   \n2   67       120   229      129      2.6       0       1      0      0      0   \n3   37       130   250      187      3.5       0       1      0      0      0   \n4   41       130   204      172      1.4       1       0      0      0      1   \n\n   ...  slope__3  ca__0  ca__1  ca__2  ca__3  thal__0  thal__1  thal__2  \\\n0  ...         1      1      0      0      0        0        0        1   \n1  ...         0      0      0      0      1        0        0        0   \n2  ...         0      0      0      1      0        0        0        0   \n3  ...         1      1      0      0      0        0        0        0   \n4  ...         0      1      0      0      0        0        0        0   \n\n   thal__3  thal__4  \n0        0        0  \n1        1        0  \n2        0        1  \n3        1        0  \n4        1        0  \n\n[5 rows x 31 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>age</th>\n      <th>trestbps</th>\n      <th>chol</th>\n      <th>thalach</th>\n      <th>oldpeak</th>\n      <th>sex__0</th>\n      <th>sex__1</th>\n      <th>cp__0</th>\n      <th>cp__1</th>\n      <th>cp__2</th>\n      <th>...</th>\n      <th>slope__3</th>\n      <th>ca__0</th>\n      <th>ca__1</th>\n      <th>ca__2</th>\n      <th>ca__3</th>\n      <th>thal__0</th>\n      <th>thal__1</th>\n      <th>thal__2</th>\n      <th>thal__3</th>\n      <th>thal__4</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>63</td>\n      <td>145</td>\n      <td>233</td>\n      <td>150</td>\n      <td>2.3</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>...</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>67</td>\n      <td>160</td>\n      <td>286</td>\n      <td>108</td>\n      <td>1.5</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>67</td>\n      <td>120</td>\n      <td>229</td>\n      <td>129</td>\n      <td>2.6</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>37</td>\n      <td>130</td>\n      <td>250</td>\n      <td>187</td>\n      <td>3.5</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>...</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>41</td>\n      <td>130</td>\n      <td>204</td>\n      <td>172</td>\n      <td>1.4</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>...</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 31 columns</p>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_inputs1=pd.get_dummies(df_inputs,columns=['sex','cp','fbs','restecg','exang','slope','ca','thal'],prefix_sep='__')\n",
    "df_inputs1[:5]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T05:13:01.408007300Z",
     "start_time": "2023-10-10T05:13:01.266005100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "X = np.array(df_inputs1)\n",
    "y = np.array(df_labels)\n",
    "X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=0,shuffle=True,test_size=0.33)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T09:08:22.155668700Z",
     "start_time": "2023-10-10T09:08:22.125674400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度 0.85\n"
     ]
    }
   ],
   "source": [
    "from sklearn.svm import SVC\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "pipe_svc = Pipeline([('scaler',StandardScaler()),('svc',SVC())])\n",
    "pipe_svc.fit(X_train,y_train)\n",
    "print(\"测试集精度\",pipe_svc.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T09:08:25.688882100Z",
     "start_time": "2023-10-10T09:08:25.441889400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合: {'svc__C': 1, 'svc__gamma': 0.01}\n",
      "最佳验证集精度: 0.8329268292682928\n",
      "测试集精度: 0.85\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "params_grid = {'svc__gamma':[0.001,0.01,0.1,1],'svc__C':[0.01,0.1,1,10]}\n",
    "pipe_svc = Pipeline([('scaler',StandardScaler()),('svc',SVC())])\n",
    "grid_svc = GridSearchCV(pipe_svc,param_grid=params_grid)\n",
    "grid_svc.fit(X_train,y_train)\n",
    "print(\"最佳参数组合:\",grid_svc.best_params_)\n",
    "print(\"最佳验证集精度:\",grid_svc.best_score_)\n",
    "print(\"测试集精度:\",grid_svc.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T09:08:33.497003400Z",
     "start_time": "2023-10-10T09:08:32.491963900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度 0.85\n"
     ]
    }
   ],
   "source": [
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "pipe_mlp = Pipeline([('scaler',StandardScaler()),('mlp',MLPClassifier(max_iter=700))])\n",
    "pipe_mlp.fit(X_train,y_train)\n",
    "print('测试集精度',pipe_mlp.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T09:08:42.613804700Z",
     "start_time": "2023-10-10T09:08:40.661033700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 调试神经网络参数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合: {'mlp__alpha': 0.01, 'mlp__hidden_layer_sizes': (100,), 'mlp__learning_rate_init': 0.1}\n",
      "最佳验证集: 0.8378048780487806\n",
      "测试集精度 0.83\n"
     ]
    }
   ],
   "source": [
    "pipe_mlp = Pipeline([('scaler',StandardScaler()),('mlp',MLPClassifier())])\n",
    "params_grid = {\n",
    "    'mlp__learning_rate_init':[0.001,0.01,0.1,1],\n",
    "    'mlp__hidden_layer_sizes':[(100,),(100,100),(75,)],\n",
    "    'mlp__alpha':[0.0001,0.001,0.01,0.1]\n",
    "}\n",
    "grid_mlp = GridSearchCV(pipe_mlp,param_grid=params_grid,n_jobs=-1)\n",
    "grid_mlp.fit(X_train,y_train)\n",
    "print(\"最佳参数组合:\",grid_mlp.best_params_)\n",
    "print('最佳验证集:',grid_mlp.best_score_)\n",
    "print(\"测试集精度\",grid_mlp.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T09:23:50.184812500Z",
     "start_time": "2023-10-10T09:23:30.658812500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(203, 31)\n",
      "(100, 31)\n"
     ]
    }
   ],
   "source": [
    "print(X_train.shape)\n",
    "print(X_test.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T09:09:40.059225200Z",
     "start_time": "2023-10-10T09:09:40.032203700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 尝试使用逻辑回归"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度: 0.85\n",
      "交叉验证\n",
      "[0.82926829 0.73170732 0.85365854 0.9        0.875     ]\n",
      "0.8379268292682926\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import cross_validate\n",
    "pipe_lr = Pipeline([('scaler',StandardScaler()),('lr',LogisticRegression())])\n",
    "pipe_lr.fit(X_train,y_train)\n",
    "print('测试集精度:',pipe_lr.score(X_test,y_test))\n",
    "print(\"交叉验证\")\n",
    "res=cross_validate(pipe_lr,X_train,y_train,cv=5)\n",
    "print(res['test_score'])\n",
    "print(np.mean(res['test_score']))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T09:24:43.557911700Z",
     "start_time": "2023-10-10T09:24:43.479924Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合 {'lr__C': 1}\n",
      "验证集的最佳精度 0.8379268292682926\n",
      "测试集精度: 0.85\n"
     ]
    }
   ],
   "source": [
    "# 尝试为逻辑回归调参\n",
    "params_grid = {'lr__C':[0.01,0.1,1,10,100]}\n",
    "pipe_lr = Pipeline([('scaler',StandardScaler()),('lr',LogisticRegression())])\n",
    "grid_lr = GridSearchCV(pipe_lr,param_grid=params_grid,cv=5)\n",
    "grid_lr.fit(X_train,y_train)\n",
    "print('最佳参数组合',grid_lr.best_params_)\n",
    "print(\"验证集的最佳精度\",grid_lr.best_score_)\n",
    "print(\"测试集精度:\",grid_lr.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T09:24:48.363885700Z",
     "start_time": "2023-10-10T09:24:48.065897Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度 0.86\n",
      "交叉验证\n",
      "[0.82926829 0.70731707 0.87804878 0.825      0.775     ]\n",
      "0.8029268292682927\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "rf = RandomForestClassifier(max_depth=5)\n",
    "rf.fit(X_train,y_train)\n",
    "print(\"测试集精度\",rf.score(X_test,y_test))\n",
    "print('交叉验证')\n",
    "res = cross_validate(rf,X_train,y_train,cv=5)\n",
    "print(res['test_score'])\n",
    "print(np.mean(res['test_score']))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T09:25:11.625832900Z",
     "start_time": "2023-10-10T09:25:10.297841200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合: {'max_depth': 4, 'n_estimators': 150}\n",
      "最佳验证集精度: 0.8129268292682926\n",
      "最佳测试集精度: 0.87\n"
     ]
    }
   ],
   "source": [
    "params = {'n_estimators':[100,120,150,180],'max_depth':[4,5,6,7]}\n",
    "grid_rf = GridSearchCV(RandomForestClassifier(),param_grid=params,n_jobs=-1)\n",
    "grid_rf.fit(X_train,y_train)\n",
    "print('最佳参数组合:',grid_rf.best_params_)\n",
    "print('最佳验证集精度:',grid_rf.best_score_)\n",
    "print('最佳测试集精度:',grid_rf.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-10T09:32:58.418223700Z",
     "start_time": "2023-10-10T09:32:49.036718600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 尝试使用召回率"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   age  sex  cp  trestbps  chol  fbs  restecg  thalach  exang  oldpeak  slope  \\\n",
      "0   63    1   1       145   233    1        2      150      0      2.3      3   \n",
      "1   67    1   4       160   286    0        2      108      1      1.5      2   \n",
      "2   67    1   4       120   229    0        2      129      1      2.6      2   \n",
      "3   37    1   3       130   250    0        0      187      0      3.5      3   \n",
      "4   41    0   2       130   204    0        2      172      0      1.4      1   \n",
      "\n",
      "   ca        thal  target  \n",
      "0   0       fixed       0  \n",
      "1   3      normal       1  \n",
      "2   2  reversible       0  \n",
      "3   0      normal       0  \n",
      "4   0      normal       0  \n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "filename = \"F:/机器学习数据集/心脏病预测/heart/heart.csv\"\n",
    "data = pd.read_csv(filename)\n",
    "print(data[:5])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T02:07:45.836214900Z",
     "start_time": "2023-10-13T02:07:45.791214500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "   age  sex  cp  trestbps  chol  fbs  restecg  thalach  exang  oldpeak  slope  \\\n0   63    1   1       145   233    1        2      150      0      2.3      3   \n1   67    1   4       160   286    0        2      108      1      1.5      2   \n2   67    1   4       120   229    0        2      129      1      2.6      2   \n3   37    1   3       130   250    0        0      187      0      3.5      3   \n4   41    0   2       130   204    0        2      172      0      1.4      1   \n\n   ca  thal  target  \n0   0     2       0  \n1   3     3       1  \n2   2     4       0  \n3   0     3       0  \n4   0     3       0  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>age</th>\n      <th>sex</th>\n      <th>cp</th>\n      <th>trestbps</th>\n      <th>chol</th>\n      <th>fbs</th>\n      <th>restecg</th>\n      <th>thalach</th>\n      <th>exang</th>\n      <th>oldpeak</th>\n      <th>slope</th>\n      <th>ca</th>\n      <th>thal</th>\n      <th>target</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>63</td>\n      <td>1</td>\n      <td>1</td>\n      <td>145</td>\n      <td>233</td>\n      <td>1</td>\n      <td>2</td>\n      <td>150</td>\n      <td>0</td>\n      <td>2.3</td>\n      <td>3</td>\n      <td>0</td>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>67</td>\n      <td>1</td>\n      <td>4</td>\n      <td>160</td>\n      <td>286</td>\n      <td>0</td>\n      <td>2</td>\n      <td>108</td>\n      <td>1</td>\n      <td>1.5</td>\n      <td>2</td>\n      <td>3</td>\n      <td>3</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>67</td>\n      <td>1</td>\n      <td>4</td>\n      <td>120</td>\n      <td>229</td>\n      <td>0</td>\n      <td>2</td>\n      <td>129</td>\n      <td>1</td>\n      <td>2.6</td>\n      <td>2</td>\n      <td>2</td>\n      <td>4</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>37</td>\n      <td>1</td>\n      <td>3</td>\n      <td>130</td>\n      <td>250</td>\n      <td>0</td>\n      <td>0</td>\n      <td>187</td>\n      <td>0</td>\n      <td>3.5</td>\n      <td>3</td>\n      <td>0</td>\n      <td>3</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>41</td>\n      <td>0</td>\n      <td>2</td>\n      <td>130</td>\n      <td>204</td>\n      <td>0</td>\n      <td>2</td>\n      <td>172</td>\n      <td>0</td>\n      <td>1.4</td>\n      <td>1</td>\n      <td>0</td>\n      <td>3</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标准化\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "label_encoder = LabelEncoder()\n",
    "label_encoder.fit(data['thal'])\n",
    "data['thal'] = label_encoder.transform(data['thal'])\n",
    "data[:5]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T02:07:56.993621400Z",
     "start_time": "2023-10-13T02:07:48.533071900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(303, 13) (303,)\n"
     ]
    }
   ],
   "source": [
    "data_X = data.iloc[:,0:-1]\n",
    "data_Y = data.iloc[:,-1]\n",
    "print(data_X.shape,data_Y.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T02:08:03.372494600Z",
     "start_time": "2023-10-13T02:08:03.361511900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "   age  trestbps  chol  thalach  oldpeak  sex__0  sex__1  cp__0  cp__1  cp__2  \\\n0   63       145   233      150      2.3       0       1      0      1      0   \n1   67       160   286      108      1.5       0       1      0      0      0   \n2   67       120   229      129      2.6       0       1      0      0      0   \n3   37       130   250      187      3.5       0       1      0      0      0   \n4   41       130   204      172      1.4       1       0      0      0      1   \n\n   ...  slope__3  ca__0  ca__1  ca__2  ca__3  thal__0  thal__1  thal__2  \\\n0  ...         1      1      0      0      0        0        0        1   \n1  ...         0      0      0      0      1        0        0        0   \n2  ...         0      0      0      1      0        0        0        0   \n3  ...         1      1      0      0      0        0        0        0   \n4  ...         0      1      0      0      0        0        0        0   \n\n   thal__3  thal__4  \n0        0        0  \n1        1        0  \n2        0        1  \n3        1        0  \n4        1        0  \n\n[5 rows x 31 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>age</th>\n      <th>trestbps</th>\n      <th>chol</th>\n      <th>thalach</th>\n      <th>oldpeak</th>\n      <th>sex__0</th>\n      <th>sex__1</th>\n      <th>cp__0</th>\n      <th>cp__1</th>\n      <th>cp__2</th>\n      <th>...</th>\n      <th>slope__3</th>\n      <th>ca__0</th>\n      <th>ca__1</th>\n      <th>ca__2</th>\n      <th>ca__3</th>\n      <th>thal__0</th>\n      <th>thal__1</th>\n      <th>thal__2</th>\n      <th>thal__3</th>\n      <th>thal__4</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>63</td>\n      <td>145</td>\n      <td>233</td>\n      <td>150</td>\n      <td>2.3</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>...</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>67</td>\n      <td>160</td>\n      <td>286</td>\n      <td>108</td>\n      <td>1.5</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>67</td>\n      <td>120</td>\n      <td>229</td>\n      <td>129</td>\n      <td>2.6</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>37</td>\n      <td>130</td>\n      <td>250</td>\n      <td>187</td>\n      <td>3.5</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>...</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>41</td>\n      <td>130</td>\n      <td>204</td>\n      <td>172</td>\n      <td>1.4</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>...</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 31 columns</p>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 独热编码\n",
    "data_X_oneHot = pd.get_dummies(data_X,columns=['sex','cp','fbs','restecg','exang','slope','ca','thal'],prefix_sep='__')\n",
    "data_X_oneHot[:5]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T02:08:39.299319700Z",
     "start_time": "2023-10-13T02:08:39.203824300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(203, 31) (100, 31)\n",
      "(100, 31) (100, 31)\n"
     ]
    }
   ],
   "source": [
    "# 划分数据集\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train,X_test,y_train,y_test = train_test_split(data_X_oneHot,data_Y,test_size=0.33,\n",
    "                                                 random_state=0,stratify=data_Y,shuffle=True)\n",
    "print(X_train.shape,X_test.shape)\n",
    "print(X_test.shape,X_test.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T02:08:46.204333800Z",
     "start_time": "2023-10-13T02:08:44.239066100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 使用逻辑斯蒂回归"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "交叉验证的10个精度 [0.80952381 0.85714286 0.85714286 0.95       0.8        0.75\n",
      " 0.85       0.8        0.9        0.85      ]\n",
      "交叉验证精度平均值 0.8423809523809525\n",
      "测试集精度 0.81\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import cross_validate\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "import numpy as np\n",
    "\n",
    "lr = LogisticRegression()\n",
    "scaler = StandardScaler()\n",
    "pipe_lr = Pipeline([('scaler',scaler),('lr',lr)])\n",
    "#pipe_lr.fit(X_train,y_train)\n",
    "scores = cross_validate(pipe_lr,X_train,y_train,n_jobs=-1,cv=10)['test_score']\n",
    "print('交叉验证的10个精度',scores)\n",
    "print('交叉验证精度平均值',np.mean(scores))\n",
    "# 测试集精度\n",
    "pipe_lr.fit(X_train,y_train)\n",
    "print(\"测试集精度\",pipe_lr.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-12T06:08:24.367162100Z",
     "start_time": "2023-10-12T06:08:24.182167800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "0.6428571428571429"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算召回率\n",
    "from sklearn.metrics import recall_score\n",
    "y_pred = pipe_lr.predict(X_test)\n",
    "recall_score(y_pred,y_test)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-12T06:10:50.902256800Z",
     "start_time": "2023-10-12T06:10:50.422891600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "0.6666666666666666"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算查准率\n",
    "from sklearn.metrics import precision_score\n",
    "y_pred = pipe_lr.predict(X_test)\n",
    "precision_score(y_pred,y_test)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-12T06:12:02.529707400Z",
     "start_time": "2023-10-12T06:12:02.488704200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数 {'lr__C': 0.1}\n",
      "验证集参数 0.8621951219512194\n",
      "测试集精度 0.85\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "params_grid = {'lr__C':[0.0001,0.001,0.01,0.1,1,10]}\n",
    "grid_lr = GridSearchCV(pipe_lr,param_grid=params_grid,n_jobs=-1)\n",
    "grid_lr.fit(X_train,y_train)\n",
    "print(\"最佳参数\",grid_lr.best_params_)\n",
    "print('验证集参数',grid_lr.best_score_)\n",
    "print(\"测试集精度\",grid_lr.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-12T06:22:35.822444100Z",
     "start_time": "2023-10-12T06:22:32.647442700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "0.7142857142857143"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recall_score(grid_lr.predict(X_test),y_test)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-12T06:23:50.981053900Z",
     "start_time": "2023-10-12T06:23:50.962019300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "0.7407407407407407"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "precision_score(grid_lr.predict(X_test),y_test)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-12T06:24:16.433159500Z",
     "start_time": "2023-10-12T06:24:16.396150100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度: 0.82\n"
     ]
    }
   ],
   "source": [
    "## 尝试使用支持向量机\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.pipeline import Pipeline\n",
    "\n",
    "scaler = StandardScaler()\n",
    "svc_classification = SVC()\n",
    "pipe_svc = Pipeline([('scaler',scaler),('svc',SVC())])\n",
    "pipe_svc.fit(X_train,y_train)\n",
    "print('测试集精度:',pipe_svc.score(X_test,y_test))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:14:32.651421900Z",
     "start_time": "2023-10-13T03:14:32.625390600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "召回率: 0.6551724137931034\n"
     ]
    }
   ],
   "source": [
    "# 召回率\n",
    "from sklearn.metrics import recall_score\n",
    "recall=recall_score(pipe_svc.predict(X_test),y_test)\n",
    "print('召回率:',recall)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:14:33.914231300Z",
     "start_time": "2023-10-13T03:14:33.881230400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合: {'svc__C': 10, 'svc__gamma': 0.001}\n",
      "验证集最佳精度: 0.8576190476190476\n",
      "测试集: 0.84\n"
     ]
    }
   ],
   "source": [
    "# 支持向量机超参数调优\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "pipe_svc = Pipeline([('scaler',scaler),('svc',SVC())])\n",
    "params_grid = {'svc__gamma':[0.001,0.01,0.1,1,10,100],'svc__C':[0.001,0.01,0.1,1,10,100]}\n",
    "grid_svc = GridSearchCV(pipe_svc,param_grid=params_grid,n_jobs=-1,cv=10)\n",
    "grid_svc.fit(X_train,y_train)\n",
    "print('最佳参数组合:',grid_svc.best_params_)\n",
    "print('验证集最佳精度:',grid_svc.best_score_)\n",
    "\n",
    "print('测试集:',grid_svc.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:14:46.437390800Z",
     "start_time": "2023-10-13T03:14:41.834397100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合 {'svc__C': 8, 'svc__gamma': 0.001}\n",
      "最佳验证集精度 0.8626190476190476\n",
      "测试集精度 0.84\n"
     ]
    }
   ],
   "source": [
    "# 尝试缩小范围\n",
    "import random\n",
    "params_grid = {'svc__C':[5,6,7,8],'svc__gamma':[0.001]}\n",
    "grid_svc = GridSearchCV(pipe_svc,param_grid=params_grid,n_jobs=-1,cv=10)\n",
    "grid_svc.fit(X_train,y_train)\n",
    "print('最佳参数组合',grid_svc.best_params_)\n",
    "print('最佳验证集精度',grid_svc.best_score_)\n",
    "print('测试集精度',grid_svc.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:14:49.187237600Z",
     "start_time": "2023-10-13T03:14:48.967245900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合 {'svc__C': 8, 'svc__gamma': 0.001}\n",
      "最佳验证集精度 0.8626190476190476\n",
      "测试集精度 0.84\n"
     ]
    }
   ],
   "source": [
    "params_grid = {'svc__C':[8],'svc__gamma':[0.001,0.003,0.005,0.01,0.015]}\n",
    "grid_svc = GridSearchCV(pipe_svc,param_grid=params_grid,n_jobs=-1,cv=10)\n",
    "grid_svc.fit(X_train,y_train)\n",
    "print('最佳参数组合',grid_svc.best_params_)\n",
    "print('最佳验证集精度',grid_svc.best_score_)\n",
    "print('测试集精度',grid_svc.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:15:05.037205300Z",
     "start_time": "2023-10-13T03:15:04.768205300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度 0.84\n"
     ]
    }
   ],
   "source": [
    "# 训练\n",
    "pipe_svc=Pipeline([('scaler',scaler),('svc',SVC(C=8,gamma=0.001))])\n",
    "pipe_svc.fit(X_train,y_train)\n",
    "print('测试集精度',pipe_svc.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:15:25.963664800Z",
     "start_time": "2023-10-13T03:15:25.930278700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SVC召回率： 0.6896551724137931\n"
     ]
    }
   ],
   "source": [
    "print('SVC召回率：',recall_score(pipe_svc.predict(X_test),y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:15:30.132540Z",
     "start_time": "2023-10-13T03:15:30.113541300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(203, 31)\n",
      "(100, 31)\n"
     ]
    }
   ],
   "source": [
    "# 重新划分数据集与训练集\n",
    "X_train,X_test,y_train,y_test = train_test_split(data_X_oneHot,data_Y,\n",
    "                                                 random_state=0,stratify=data_Y,\n",
    "                                                 test_size=0.33)\n",
    "print(X_train.shape)\n",
    "print(X_test.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:13:15.879340900Z",
     "start_time": "2023-10-13T03:13:15.871301600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度 0.8\n"
     ]
    }
   ],
   "source": [
    "## 尝试使用神经网络\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "\n",
    "mlp = MLPClassifier(max_iter=1000)\n",
    "pipe_mlp = Pipeline([('scaler',scaler),('mlp',mlp)])\n",
    "pipe_mlp.fit(X_train,y_train)\n",
    "print('测试集精度',pipe_mlp.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:15:37.397725200Z",
     "start_time": "2023-10-13T03:15:36.558725800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "召回率: 0.6060606060606061\n"
     ]
    }
   ],
   "source": [
    "print('召回率:',recall_score(pipe_mlp.predict(X_test),y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:15:39.715608Z",
     "start_time": "2023-10-13T03:15:39.696571200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合 {'mlp__hidden_layer_sizes': (50, 50), 'mlp__learning_rate_init': 0.1}\n",
      "验证集最佳精度 0.8716666666666667\n",
      "测试集精度 0.77\n"
     ]
    }
   ],
   "source": [
    "# 神经网络调参\n",
    "params_grid = {'mlp__learning_rate_init':[0.0001,0.001,0.01,0.1],'mlp__hidden_layer_sizes':[(100,),(50,),(50,50),(100,100)]}\n",
    "grid_mlp = GridSearchCV(pipe_mlp,param_grid=params_grid,n_jobs=-1,cv=10)\n",
    "grid_mlp.fit(X_train,y_train)\n",
    "print('最佳参数组合',grid_mlp.best_params_)\n",
    "print(\"验证集最佳精度\",grid_mlp.best_score_)\n",
    "print('测试集精度',grid_mlp.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:30:06.026457Z",
     "start_time": "2023-10-13T03:29:36.508431Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "outputs": [
    {
     "data": {
      "text/plain": "0.5161290322580645"
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mlp = MLPClassifier(max_iter=1000,hidden_layer_sizes=(50,50),learning_rate_init=0.1)\n",
    "pipe_mlp = Pipeline([('scaler',scaler),('mlp',mlp)])\n",
    "pipe_mlp.fit(X_train,y_train)\n",
    "recall_score(pipe_mlp.predict(X_test),y_test)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:34:32.537033200Z",
     "start_time": "2023-10-13T03:34:31.811036500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度 0.74\n"
     ]
    }
   ],
   "source": [
    "## 梯度提升树\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "grid = GradientBoostingClassifier()\n",
    "grid.fit(X_train,y_train)\n",
    "print('测试集精度',grid.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:15:41.165713100Z",
     "start_time": "2023-10-13T03:15:41.068711800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度 0.8\n"
     ]
    }
   ],
   "source": [
    "# 随机森林\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "rf = RandomForestClassifier(max_depth=4)\n",
    "rf.fit(\n",
    "    X_train,y_train\n",
    ")\n",
    "print(\"测试集精度\",rf.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:56:20.293282200Z",
     "start_time": "2023-10-13T03:56:20.120283400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机森林召回率 0.6296296296296297\n"
     ]
    }
   ],
   "source": [
    "print(\"随机森林召回率\",recall_score(rf.predict(X_test),y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-13T03:57:42.515103400Z",
     "start_time": "2023-10-13T03:57:42.486084900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
